package com.ml.springbootidus.cotroller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ml.springbootidus.mapper.UserInfosMapper;
import com.ml.springbootidus.mapper.UsertestMapper;
import com.ml.springbootidus.pojo.UserInfo;
import com.ml.springbootidus.service.UserInfosService;
import io.github.yedaxia.apidocs.ApiDoc;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 用户接口【Mybatis-Plus】
 */
@RestController
public class UserInfosController {

    @Autowired
    UserInfosMapper userInfosMapper;

    @Autowired
    UserInfosService userInfosService;

    @Autowired
    UsertestMapper usertestMapper;

    /**
     * 获取用户信息
     */
    @GetMapping("/getusers")
    public List<UserInfo> getUsers() {
        List<UserInfo> userList = userInfosMapper.selectList(new QueryWrapper<>());
        userList.forEach(System.out::println);
        if (userList != null) {
            return userList;
        } else {
            return null;
        }
    }

    /**
     * 删除用户信息
     *
     * @param userid 用户ID
     */
    @DeleteMapping("/strikeuser/{userid}")
    @ApiDoc(result = boolean.class, url = "http://localhost:8080/strikeuser/{userid}", method = "delete")
    public String strikeUser(@PathVariable("userid") String userid) {
        int update = userInfosMapper.deleteById(userid);
        if (update == 1) {
            return "已经删除：请访问【http://localhost:8080/listuser】";
        } else {
            return "删除失败！！";
        }
    }

    /**
     * 修改用户信息
     *
     * @param userid 用户ID
     * @param uname  用户名称
     * @param usex   用户性别
     */
    @PutMapping("/modifyuser")
    @ApiDoc(result = boolean.class, url = "http://localhost:8080/modifyuser", method = "put")
    public String modifyuser(@RequestParam("userid") String userid,
                             @RequestParam("uname") String uname,
                             @RequestParam("usex") String usex) {
        UserInfo userInfos = new UserInfo();
        userInfos.setUname(uname);
        userInfos.setUsex(usex);
        UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<UserInfo>();
        updateWrapper.eq("userid", userid);
        Integer update = userInfosMapper.update(userInfos, updateWrapper);
        if (update == 1) {
            return "已经修改：请访问【http://localhost:8080/listuser】";
        } else {
            return "修改失败！！";
        }
    }

    /**
     * 添加用户信息
     *
     * @param uname        用户名称
     * @param usex         用户性别
     * @param udescription 用户描述/评价
     * @return
     */
    @PostMapping("/adduser")
    @ApiDoc(result = Boolean.class, url = "http://localhost:8080/adduser", method = "post")
    public String addUserInfo(@RequestParam("uname") String uname,
                              @RequestParam("usex") String usex,
                              @RequestParam("udescription") String udescription) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUname(uname);
        userInfo.setUsex(usex);
        userInfo.setUdescription(udescription);
        boolean save = userInfosService.save(userInfo);
        if (true == save) {
            return "添加成功！！";
        } else {
            return "添加失败！！";
        }
    }

    /**
     * 添加用户信息【带时间】
     *
     * @param uname
     * @param usex
     * @param udescription
     */
    //添加【带时间】
    @PostMapping("/addusers")
    @ApiDoc(result = boolean.class, url = "http://localhost:8080/addusers", method = "post")
    public String addUserInfos(@RequestParam("uname") String uname,
                               @RequestParam("usex") String usex,
                               @RequestParam("udescription") String udescription) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUname(uname);
        userInfo.setUsex(usex);
        userInfo.setUdescription(udescription);
        userInfo.setCreattime(new Date());
        // userInfo.setCreattime(creattime);//2020-01-02
        boolean save = userInfosService.save(userInfo);
        if (save == true) {
            return "添加成功！！";
        } else {
            return "添加失败！！";
        }
    }
}
